<template>
	<view class="padding-lr-sm padding-top box bg-white radius-16" v-if="lists.length > 0">
		<view class="flex align-center justify-between margin-bottom-lg">
			<view class="flex align-center">
				<image class="tit-icon" :src="imgBaseUrl + 'index/icon-6.png'"></image>
				<ben-count-down ref="count" @refresh="init" :seckillColor="'#F02325'"></ben-count-down>
			</view>
      <view class="flex align-center" @tap="handleJump" data-url="/pages/activity/seckill/seckill-list/index">
        <text class="text-sm" style="color: #B1B4BA;">查看更多</text>
        <text class="cuIcon-right margin-left-xs" style="font-size: 20rpx; color: #B1B4BA;"></text>
      </view>
		</view>
		<view class="flex">
			<block v-for="(item,index) in nowTabs" :key="index">
				<view class="flex-sub flex align-center justify-center flex-direction" style="min-width: 1px;" @click="tabSelect(index)">
					<text class="text-bold" :class="[index == curIndex ? 'text-prices fu-fs40' : 'fu-fs30 text-333']">{{item.model}}</text>
					<text class="fu-fs20" :class="[index == curIndex ? 'text-white bg-prices padding-lr-15 round line-height-32 height-32' : 'text-999']">{{index == 0 ? '抢购中' : '即将开始'}}</text>
				</view>
			</block>
		</view>
		<view>
			<view class="margin-top-sm relative" v-if="lists.length > 0">
				<block v-for="(item,index) in lists" :key="index">
					<view class="list-item padding-tb-sm flex solid-bottom" @click="navToDetail(item)">
						<view class="bg-f8 fu-block-246 margin-right-sm relative">
							<view class="top-left-badge fu-fs20 text-white width-80 height-40 bg-prices flex align-center justify-center">
								疯抢
							</view>
							<image class="fu-block-246 radius-8" :src="item.thumb" mode="aspectFill" lazy-load></image>
						</view>
						<view class="flex-sub flex flex-direction justify-between">
							<view class="fu-fs28 text-333 text-cut-2 text-bold">{{item.name}}</view>
							<view class="flex align-center" v-if="curIndex == 0">
								<view class="cu-progress round xs" style="width: 65%;">
									<view class="bg-theme" :style="{ width: item.rate + '%' }"></view>
								</view>
								<text class="text-999 text-xs margin-left-sm">{{item.rate + '%'}}</text>
							</view>
							<view class="text-xs text-999 text-cut" v-if="curIndex == 0">
								<text>{{item.sales_sum}}人成功秒杀</text>
								<text v-if="(item.sales_sum != 0) && (item.discounts != 0)" class="margin-left-xs">
									成功为他们省了{{item.discounts | filterPrice}}元
								</text>
							</view>
							<view class="flex align-center justify-between">
								<view class="flex flex-direction">
									<view class="text-cut">
										<text class="text-price text-sm text-prices"></text>
										<text class="text-xl text-prices text-bold text-cut">{{item | filterGoodsPrice}}</text>
									</view>
									<view class="text-through text-sm text-999">
										<text class="text-price">{{item.market_price}}</text>
									</view>
								</view>
								<view class="action flex" v-if="curIndex == 0">
									<!-- <view class="width-70 flex align-center justify-center flex-direction fu-fs20" style="color: #B16E05;">
										<text>返</text>
										<text class="text-bold">{{item.discounts | filterPrice}}</text>
									</view> -->
									<view class="flex-sub text-white text-center" style="min-width: 1px;" v-if="item.rate !== 100">
										<text class="title">马上抢</text>
									</view>
									<view class="flex-sub text-white text-center" v-else @tap.stop="">
										<text class="title">抢光了</text>
									</view>
								</view>
								<view v-else>
                  <view class="bg-theme text-white width-140 fu-fs24 radius-8 flex align-center justify-center">
                  	<text class="title" @click.stop="">敬请期待</text>
                  </view>
<!--                  <view class="bg-theme text-white width-140 height-56 line-height-56 fu-fs28 radius-8 flex align-center justify-center" v-if="!(item.message_notify && item.message_notify == 'true')">
                  	<text class="title" @click.stop="handleRemind(item)">提醒我</text>
                  </view>
                  <view class="line-theme solid width-140 height-56 line-height-56 fu-fs28 radius-8 flex align-center justify-center" v-else>
                  	<text class="title" @click.stop="handleUnRemind(item)">取消提醒</text>
                  </view> -->
								</view>
							</view>
						</view>
					</view>
				</block>
			</view>
			<view v-else class="height-150 fu-fs24 text-999 text-center text-bold flex align-center justify-center">
				亲，这个时间段没有要秒杀的商品噢~
			</view>
		</view>
	</view>
</template>

<script>
	import benCountDown from '../ben-count-down/ben-count-down.vue';
	const _tabs = [{
			model: '00:00',
			text: '即将开始',
			start: 0
		},
		{
			model: '02:00',
			text: '即将开始',
			start: 2
		},
		{
			model: '04:00',
			text: '即将开始',
			start: 4
		},
		{
			model: '06:00',
			text: '即将开始',
			start: 6
		},
		{
			model: '08:00',
			text: '即将开始',
			start: 8
		},
		{
			model: '10:00',
			text: '即将开始',
			start: 10
		},
		{
			model: '12:00',
			text: '即将开始',
			start: 12
		},
		{
			model: '14:00',
			text: '即将开始',
			start: 14
		},
		{
			model: '16:00',
			text: '即将开始',
			start: 16
		},
		{
			model: '18:00',
			text: '即将开始',
			start: 18
		},
		{
			model: '20:00',
			text: '即将开始',
			start: 20
		},
		{
			model: '22:00',
			text: '即将开始',
			start: 22
		},
	];
	export default {
		components: {
			benCountDown,
		},
		data() {
			return {
				lists: [],
				tabs: Object.freeze(_tabs),
				nowTabs: [],
				curIndex: 0,
				start: null
			}
		},
		methods: {
			init() {
				let nowDate = new Date();
				let _hour = nowDate.getHours();
				let _count;
				if (_hour % 2 === 0) {
					_count = _hour;
				} else {
					_count = _hour - 1;
				}
				let _startIndex = _count / 2;
				const _tabs = this.tabs.filter((item, index) => {
					return index >= _startIndex && index <= _startIndex + 4
				})
				if (_tabs.length < 5) {
					let _len = 5 - _tabs.length;
					for (let i = 0; i < _len; i++) {
						_tabs.push(this.tabs[i])
					}
				}
				this.nowTabs = _tabs;
				this.start = this.nowTabs[0].start;
				this.getSeckill();
			},
			// 获取秒杀活动
			getSeckill() {
				let data = {
					start_time: this.start,
					end_time: this.start + 2,
					page: 1,
					list_rows: 5,
					type: 1,
					user_id: global.userInfo && global.userInfo.id || 0,
				}
				this.$api.post(global.apiUrls.postSeckillList,data)
				.then(res => {
					if (res.data.code == 1) {
						this.lists = res.data.data.data;
					} else if (res.code == 0) {
						this.lists = [];
					}
				})
			},
			// 去详情
			navToDetail(item) {
				if (this.curIndex != 0) return;
				this.$urouter.navigateTo({
					url: '/pages/activity/seckill/seckill-detail/index',
					params: {
						goods_id: item.id,
						activity_id: item.activity_id || 0,
						sku_id: item.sku_id,
					}
				})
			},
			tabSelect(index, start) {
				this.curIndex = index;
				this.start = this.nowTabs[index].start;
				this.getSeckill();
			},
			refresh() {
				this.init();
				this.$refs.count.refresh();
			},
			// 发送通知
			handleRemind(item) {
			  const {
			    goods_id,
			    activity_id,
			    activity_type,
			    sku_id
			  } = item;
			  this.$util.actionAuth(() => {
			    let _notifyTime = (Math.abs(this.start - 1)).toString().padStart(2, '0') + ':57'
			    // #ifdef MP-WEIXIN
			    uni.requestSubscribeMessage({
			      tmplIds: ['x3ljpUAnxYgi-u6iQcaRuP-Z3fVoJ5o22KTDqiNhPMk'],
			      complete: () => {
			        this.$api.post(global.apiUrls.postSendNotice, {
			          goodsId: goods_id,
			          pageUrl: `/pages/activity/seckill/seckill-detail?id=${goods_id}&sku_id=${sku_id}&activity_id=${activity_id}`,
			          type: 1,
			          notifyTime: _notifyTime,
			        })
			          .then(res => {
			            res = res.data;
			            console.log('消息通知', res);
			            if (res.code == 1) {
			              this.$message.info('设置成功,将在开抢前三分钟收到提醒');
			              this.getSeckill();
			            } else {
			              this.$message.info(res.msg);
			            }
			          })
			          .catch(err => {
			            this.$message.info('该功能暂未开发');
			            console.log(err);
			          })
			      },
			    })
			    // #endif
			    // #ifdef APP-PLUS
			    this.$api.post(global.apiUrls.postSendNotice, {
			      goodsId: goods_id,
			      pageUrl: `/pages/activity/seckill/seckill-detail?id=${goods_id}&sku_id=${sku_id}&activity_id=${activity_id}`,
			      type: 2,
			      notifyTime: _notifyTime,
			    })
			      .then(res => {
			        res = res.data;
			        console.log('消息通知', res);
			        if (res.code == 1) {
			          this.$message.info('设置成功,将在开抢前三分钟收到提醒');
                this.getSeckill();
			        } else {
			          this.$message.info(res.msg);
			        }
			      })
			      .catch(err => {
			        this.$message.info('该功能暂未开发');
			        console.log(err);
			      })

			    // #endif
			    // #ifdef H5
			    this.$message.info('H5不支持推送功能');
			    // #endif
			  })
			},
			// 取消通知
			handleUnRemind(item) {
			  const { goods_id } = item;
			  this.$util.actionAuth(() => {
			    let _notifyTime = (Math.abs(this.start - 1)).toString().padStart(2, '0') + ':57'
			    this.$api.post(global.apiUrls.postCancelNotice, {
			      goodsId: goods_id,
            // #ifdef MP-WEIXIN
			      type: 1,
            // #endif
            // #ifdef APP-PLUS
            type: 2,
            // #endif
			      notifyTime: _notifyTime,
			    })
			      .then(res => {
			        console.log('取消通知', res);
              res = res.data;
			        if (res.code == 1) {
			          this.$message.info('秒杀提醒已取消,您可能会抢不到哟~');
			          this.getSeckill();
			        } else {
			          this.$message.info(res.msg);
			        }
			      })
			      .catch(err => {
			        console.log(err);
			      })
			  })
			},
		},
		filters: {
			filterPrice(price) {
			  return parseFloat(Number(price));
			}
		},
		created() {
			this.init();
		}
	}
</script>

<style lang="scss" scoped>
	.box {
    .bg-prices {
      background: #F02325;
    }
	.text-prices {
		color: #F02325;
	}
		.list-item{
      .top-left-badge{
        position: absolute;
      	top: 0;
      	left: 0rpx;
      	z-index: 999;
      	border-radius: 24rpx 0rpx 24rpx 0rpx;
      }
			.action{
				// background: url($IMG_BASE_URL + 'activity/rob.png');
				// background-size: cover;
				// background-repeat: no-repeat;
				background-color: #F02325;
				border-radius: 8rpx;
				color: #fff;
				font-size: 24rpx;
				width: 120rpx;
				text-align: center;
				line-height: 48rpx;
        // .title{
        // 	animation: heart 1s infinite;
        // }
			}
			.fu-fs24 {
				font-size: 24rpx;
				line-height: 48rpx;
			}
		}
	}
	@keyframes heart{
		0%{
			font-size: 20rpx;
		}
		50%{
			font-size: 26rpx;
		}
		100%{
			font-size: 20rpx;
		}
	}
	
	.tit-icon {
		width: 24rpx;
		height: 24rpx;
		margin-right: 16rpx;
	}
	.title-xs {
		font-size: 40rpx;
		font-family: YouSheBiaoTiHei-Regular, YouSheBiaoTiHei;
		color: #333;
		margin-right: 24rpx;
		font-weight: bold;
	}
	.bg-theme {
		background-color: #F02325;
	}
	
</style>
